package com.xxxcompany.world.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.xxxcompany.utils.DbUtils;
import com.xxxcompany.world.dao.ICountryDAO;
import com.xxxcompany.world.entity.Country;

public class CountryDAOImpl implements ICountryDAO {

	@Override
	public List<Country> findAll() throws SQLException {

		List<Country> list = new ArrayList<>();
		// 1. 获取连接
		Connection conn = DbUtils.getConnection();
		// 2. 执行查询
		String sql = "select * from country";
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery(sql);

		// 3. 遍历结果集，封装结果信息
		while (rs.next()) {
			String code = rs.getString(1);
			String name = rs.getString(2);
			Country c = new Country(code,name);
			list.add(c);
		}
		// 4. 释放资源
		DbUtils.destoryResource(conn, stmt, rs);
		return list;
	}

}